﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Protal.Interfaces;
using Microhard.Core.DBUtility;
using Microhard.Protal.DataAccess;
using Microhard.Protal.DTO;
using Microhard.Protal.Resource;
using Microhard.Core;

namespace Microhard.Protal.Impl
{
    public class ServiceImpl : IService
    {
        public Core.DBUtility.ExecutedResult Add(Models.ServiceModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;

            try
            {
                db.Microhard_CustomerService.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add("IService.Add", Msg.Impl_Add);
                Log.Error("IService.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ServiceModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ServiceModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ServiceModel GetModelByID(Guid id)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var rtn = db.Microhard_CustomerService.SingleOrDefault(p => p.ServiceID == id);
            if (rtn != null)
            {
                return rtn.ToModel();
            }

            return null;
        }

        public Models.ServiceModel GetModelByModel(Models.ServiceModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ServiceModel> Query(Models.ServiceModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardProtalEntities db = new MicrohardProtalEntities();
            var entities = from b in db.Microhard_CustomerService
                           select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.CreateTime).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pagedEntities.Select(m => m.ToModel()).ToList();
        }

        public List<Models.ServiceModel> Query(Models.ServiceModel Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
